home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
422_02
/
misc
/
byteswap.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-20
|
623b
|
29 lines
/*
* Example of using a union to overlay int (16 bit) and char (8 bit)
* data. In this program, a 16 bit int is written to memory, and then
* the high and low nibbles are exchanged by reading them as chars
* defined in a union with the int.
*
* Compile command: cc byteswap -fop
*/
#include <stdio.h>
main()
{
union {
int Idata;
struct {
char byte1;
char byte2; } Bdata; } data;
int i;
char t;
for(i=0; i < 10; ++i) {
data.Idata = i;
t = data.Bdata.byte1;
data.Bdata.byte1 = data.Bdata.byte2;
data.Bdata.byte2 = t;
printf("%04x <-> %04x\n", i, data.Idata); }
}